﻿using Abp.Runtime.Validation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AVIS.OutBound.WizardAgent
{
    /// <summary>
    /// 获取车组信息的Wizard请求信息类（多个）
    /// </summary>
    public class CarGroupsRequest : ICacheRequest
    {
        #region 取还车信息
        /// <summary>
        /// 取车时间
        /// </summary>
        public DateTime PickupDateTime { get; set; }

        /// <summary>
        /// 取车门店编号
        /// </summary>
        public string PickUpStoreCode { get; set; }

        /// <summary>
        /// 还车时间
        /// </summary>
        public DateTime ReturnDateTime { get; set; }

        /// <summary>
        /// 还车门店编号
        /// </summary>
        public string ReturnStoreCode { get; set; }
        #endregion

        /// <summary>
        /// 套餐价格代码
        /// </summary>
        public string RateCode { get; set; }

        /// <summary>
        /// Wizard会员号
        /// </summary>
        public string WizardNum { get; set; }

        /// <summary>
        /// Wizard优惠券号
        /// </summary>
        public string CouponNum { get; set; }

        /// <summary>
        /// Avis全球折扣号
        /// </summary>
        public string AWD { get; set; }

        /// <summary>
        /// 协议号（IATA/ANN）
        /// </summary>
        public string IATA { get; set; }

        /// <summary>
        /// 协议类型（IATA/ANN）
        /// </summary>
        public int IATAType { get; set; }

        #region 构建XML信息
        /// <summary>
        /// 会员ID的XML
        /// </summary>
        protected string XMembershipID
        {
            get
            {
                if (!string.IsNullOrEmpty(WizardNum))
                {
                    return string.Format("  <CustLoyalty MembershipID='{0}'/>", WizardNum);
                }
                else
                {
                    return string.Empty;
                }
            }
        }

        /// <summary>
        /// 公司（旅行社、携程）的折扣号的XML
        /// </summary>
        /// <returns></returns>
        protected string XCorpDiscountNmbr
        {
            get
            {
                var xmlCorpDiscountNum = string.Empty;
                if (!string.IsNullOrEmpty(RateCode) || !string.IsNullOrEmpty(CouponNum) || !string.IsNullOrEmpty(AWD))
                {
                    var rateCategory = 6;
                    var xmlProp = "";

                    // 套餐号
                    if (!string.IsNullOrEmpty(RateCode))
                    {
                        // 需要排除德国车组，需要问张珉敏为啥？
                        if (!"^XM^JX^22^0H^".Contains(RateCode))
                        {
                            rateCategory = 2;
                        }
                        xmlProp += string.Format(" RateQualifier='{0}' ", RateCode);

                    }

                    // 优惠券号
                    if (!string.IsNullOrEmpty(CouponNum))
                    {
                        xmlProp += string.Format(" PromotionCode='{0}' ", CouponNum);
                    }

                    if (!string.IsNullOrEmpty(AWD))
                    {
                        xmlProp += string.Format(" CorpDiscountNmbr='{0}' ", AWD);
                    }

                    xmlCorpDiscountNum = string.Format("<RateQualifier {0} RateCategory='{1}'/>", xmlProp, rateCategory);
                }

                return xmlCorpDiscountNum;
            }
        }

        /// <summary>
        /// 协议号的XML
        /// </summary>
        protected string XIATAANN
        {
            get
            {
                string xmlIATAANN = string.Empty;
                if (!string.IsNullOrEmpty(IATA))
                {
                    xmlIATAANN = string.Format("<Source><RequestorID ID='{0}' Type='{1}'/></Source>", IATA, IATAType);
                }
                return xmlIATAANN;
            }
        }
        #endregion

        public virtual string GetCacheKey()
        {
            return string.Format("{0:yyyyMMddHH}_{1:yyyyMMddHH}_{2}_{3}_{4}_{5}_{6}",
                PickupDateTime, ReturnDateTime, PickUpStoreCode, ReturnStoreCode, RateCode, AWD, CouponNum);
        }

        public virtual string ToRequestXml()
        {
            var request = this;
            string xmlRequest = string.Format(WizardConsts.OTA_VehAvailRateRQ
                    , request.PickupDateTime
                    , request.ReturnDateTime
                    , request.PickUpStoreCode
                    , request.ReturnStoreCode
                    , ""
                    , request.XMembershipID
                    , request.XCorpDiscountNmbr
                    , request.XIATAANN
                    , ""
                )
                .Replace("'", "\"")
                .Replace("OutboundUser", WizardConsts.OutboundUser)
                .Replace("OutboundPwd", WizardConsts.OutboundPwd)
                .Replace("OutboundRequestID", WizardConsts.OutboundRequestID);

            return xmlRequest;
        }

        public virtual bool CheckRequest()
        {
            if (this.PickupDateTime.Ticks == 0)
            {
                throw new WizardException("无效的取车日期");
            }

            if (this.ReturnDateTime.Ticks == 0)
            {
                throw new WizardException("无效的还车日期");
            }

            if (string.IsNullOrWhiteSpace(this.PickUpStoreCode))
            {
                throw new WizardException("取车门店不能为空");
            }

            if (string.IsNullOrWhiteSpace(this.ReturnStoreCode))
            {
                throw new WizardException("还车门店不能为空");
            }
            return true;
        }
    }
}